import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description :
 * User: $ {USER}
 * Date: $ {YEAR}-$ {MONTH}一$ { DAY}
 * Time: ${ TIME}
 */

/**
 * 题目：LCR 123. 图书整理I
 * 思路：
 *     1.既然是倒序存储，就应该要联想到我们学过的栈，将链表遍历，存储到栈中后，再用数组来接收栈中的元素
 *     2.直接用顺序表存储，后倒序存储到数组中即可
 */
public class Test {
    public int[] reverseBookList(ListNode head) {
        // 倒序输出——联想到栈
        Stack<Integer> stack = new Stack<>();
        ListNode cur = head;
        while (cur != null) {
            stack.push(cur.val);
            cur = cur.next;
        }
        int n = stack.size();
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = stack.pop();
        }
        return array;
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}